package com.iris.live.services.report.data.matchers;

import com.iris.live.services.data.models.ReportConfig;
import com.iris.live.services.report.data.IReportDataMatcher;

import java.util.Map;

/**
 * @Description:统计数据匹配实现
 * @author: Tony.Zhang
 * @Date: 2017-08-12 12:08
 **/
public class Matcher implements IReportDataMatcher {
    @Override
    public boolean isMatched(Map data, ReportConfig config) throws Exception {
        ReportConfig matchConfig = config;
        boolean matched = false;
        String value = null;
        while(true){
            if(matchConfig.getMatchItem() == 1  && data.containsKey(matchConfig.getMatchKey()) && data.get(matchConfig.getMatchKey()) != null){
                matched = true;
                value = data.get(matchConfig.getMatchKey()).toString();

                if(matchConfig.getMatcher() == 1) { //等于
                    if (!value.equalsIgnoreCase(matchConfig.getText())) {
                        return false;
                    }
                }else if(matchConfig.getMatcher() == 2){ //包含
                    if (("+" + matchConfig.getText() + "+").indexOf("+" + value + "+") < 0) {
                        return false;
                    }
                }
            }else if(matchConfig.getMatchItem()==2){
                return true;
            }

            matchConfig = matchConfig.getParentConfig();
            if(matchConfig == null){
                break;
            }
        }

        return matched;
    }
}
